﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NewFormatProject.Infrastructure.Atrributes;
using NewFormatProject.Infrastructure.Dto.Requests.Objects;
using NewFormatProject.Server.Interfaces;
using System.Threading.Tasks;

namespace NewFormatProject.Api.Admin.Controllers
{
    [Authorize]
    [CheckLogin]
    public class DepartmentController : BaseController
    {
        private readonly IDepartmentServer _departmentServer;
        public DepartmentController(IDepartmentServer departmentServer)
        {
            _departmentServer = departmentServer;
        }
        
        /// <summary>
        /// 获取部门树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Tree()
        {
            return Ok(await _departmentServer.Tree());
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog("添加部门")]
        public async Task<IActionResult> Add(RequestSysDepartmentsDto dto)
        {
            return Ok(await _departmentServer.Add(dto));
        }

        /// <summary>
        /// 更新部门
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog("更新部门")]
        public async Task<IActionResult> Update(RequestSysDepartmentsDto dto)
        {
            return Ok(await _departmentServer.Update(dto));
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionLog("删除部门")]
        public async Task<IActionResult> Delete(RequestSysDepartmentsDto dto)
        {
            return Ok(await _departmentServer.Delete(dto));
        }
    }
}
